import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qaun_ni_dev/provider/gift_provider.dart';

class GiftDetail extends StatelessWidget {
  var giftData;
  GiftDetail(this.giftData);


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('礼物详情'),
      ),
        body: Stack(
          children: <Widget>[
            _detailContent(),
            _optionBtn(context, giftData),
          ],
        )
    );
  }

  Widget _detailContent() {
    return Container(
        padding: EdgeInsets.only(top: 10, left: 10, right: 10, bottom: 80),
        height: 610,
        child: ListView(
          children: <Widget>[
            Column(
              children: <Widget>[
                Container(
                  padding:
                  EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('礼物id:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${giftData.id}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                  EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('礼物名称:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${giftData.giftName}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding:
                  EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text('礼物价格:'),
                        width: 90,
                        margin: EdgeInsets.only(right: 10),
                      ),
                      Container(
                        width: 210,
                        child: Text('${giftData.giftPrice}'),
                      )
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
                  margin: EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(Radius.circular(5))
                  ),
                  child: Column(
                    children: <Widget>[
                      Container(
                        width: 360,
                        child:  Text('礼物图片：', textAlign: TextAlign.left,),
                      ),
                      Container(
                        child: Row(
                          children: <Widget>[
                            SizedBox(
                              width: 90,
                            ),
                            Container(
                              width: 180,
                              child: Image.network(giftData.giftImgUrl),
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              ],
            )
          ],
        ));
  }

  Widget _optionBtn(context, giftData) {
    return Positioned(
      bottom: 0,
      width: 360,
      height: 80,
      left: 40,
      child: Container(
        alignment: Alignment.center,
        child: Row(
          children: <Widget>[
            Container(
              width: 100,
              height: 40,
              padding: EdgeInsets.only(top: 10, bottom: 10),
              margin: EdgeInsets.only(right: 80),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(40)),
//                color: Colors.lightBlue,
              ),
              child: InkWell(
                onTap: () {
                  print('编辑');
//                  Navigator.push(context, MaterialPageRoute(builder: (context) {
////                    return AdminUpdate(adminData);
//                  }));
                },
                child: Text(
                  '编辑',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
            Container(
              width: 100,
              height: 40,
              padding: EdgeInsets.only(top: 10, bottom: 10),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(40)),
                color: Colors.redAccent,
              ),
              child: InkWell(
                onTap: () {
                  _deleteAdmin(context, giftData);
                },
                child: Text(
                  '删除',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _deleteAdmin(context, giftData) {
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('警告'),
          content: Text('确认要删除该条数据吗？'),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: () {
                Navigator.of(context).pop();
              } , //关闭对话框
            ),
            FlatButton(
              child: Text("确认"),
              onPressed: () {
                var formData= {
                  'giftId': giftData.id
                };
                Provider.of<GiftProvider>(context,listen: false).deleteGift(context, formData);
                Navigator.of(context).pop(); //关闭对话框
              },
            ),
          ],
        );
      },
    ).then((val) {
      print(val);
    });
  }


}
